<?php
namespace Web\Logic;
class MemberLogic {

	/******************************sql****************************************************/

	function selRoleData(){
		try {
		    $andStr = "";
            $role_id = session('roleid');
            $admin_role_id = C('ADMIN_ROLE');
            $admin_role = explode(",",$admin_role_id);
            if(!in_array($role_id,$admin_role)){
                $andStr = " and roleid not in($admin_role_id)";
            }
            //实例化模块
            $mod = M("role");
            $res =  $mod->field("roleid,rolename")
                       ->where("isforbidden = 0".$andStr)
                       ->order('roleid ASC')
                       ->select();
            return $res;
        } catch (Exception $e) {
            return "";
        }
	}

	/**
	 * 添加数据
	 * @param unknown $data
	 * @return Ambigous <mixed, boolean, unknown, string>
	 */
	function addDataSql($data){
		try {
			//实例化模块
			$mod = M("member");
		    return $mod->data($data)->add();
		} catch (Exception $e) {
		}
	}

	/**
	 * 查询个数
	 * @param unknown $data
	 */
	function selNum($data){
		try {
			//实例化模块
			$mod = M("member");
			return  $mod
            ->alias('m')
            ->where($data)->field('userid')->count();
		} catch (Exception $e) {
		}
	}

	/**
	 * 查询数据
	 * @param unknown $wdata
	 * @param unknown $Page
	 */
	function indexList($wdata,$Page){
		try {
            //实例化模块
            $mod = M("member");

            $rdata = $mod->alias('m')
						 ->join('left join '.C('DB_PREFIX')."role r ON m.roleid = r.roleid")
						 ->field("m.*,r.rolename")
                         ->where($wdata)
                         ->order('userid DESC')
                         ->limit($Page->firstRow.','.$Page->listRows)
                         ->select();
            return $rdata;
        } catch (Exception $e) {
        }
	}

	/**
	 * 删除数据
	 * @param unknown $roleid
	 */
	function delDataSql($userid){
		try {
			//实例化模块
			$mod = M("member");
			return $mod->where("userid = $userid")->delete();
		} catch (Exception $e) {
		}
	}

	/**
	 * 修改数据
	 * @param unknown $data
	 * @param unknown $roleid
	 */
    function editDataSql($data,$userid){
    	try {
    		//实例化模块
    		$mod = M("member");
    		return $mod->where("userid = '$userid'")->save($data);
    	} catch (Exception $e) {
    	}
    }

    /**
     * 修改数据
     * @param unknown $data
     * @param unknown $roleid
     */
    function selOneSql($userid){
    	try {
            //实例化模块
            $mod = M("member");
            $rdata = $mod
                    ->field('*')
                    ->where("userid = '$userid'")
                    ->find();
            return $rdata;
        } catch (Exception $e) {
        }
    }
}